package com.leyou.page.service;

import com.leyou.page.utils.ThreadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;


@Service
public class FileService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${ly.thymeleaf.destPath}")
    private String destPath;

    public void createHtml(Long id) throws IOException {
        //创建上下文对象
        Context context = new Context();

        //把数据加入上下文
        context.setVariables(this.goodsService.loadModel(id));

        //创建一个输出流,关联到一个临时文件
        File temp = new File(id + ".html");
        //目标页面文件
        File dest = createPath(id);
        //备份原页面文件
        File bak = new File(id + "_bak.html");

        try (PrintWriter writer = new PrintWriter(temp, "utf-8")) {
            //利用thymeleaf模版引擎生成静态页面
            templateEngine.process("item", context, writer);

            if (dest.exists()) {
                //如果目标文件已经存在,先备份
                dest.renameTo(bak);
            }

            //将新页面覆盖旧页面
            FileCopyUtils.copy(temp, dest);

            //覆盖成功后删除备份
           bak.delete();
        } catch (IOException e) {
            //失败后,讲备份页恢复
            bak.renameTo(dest);
            //抛出异常,声明页面生成失败
            throw new IOException(e);
        }finally {

            //删除临时页面
            if (temp.exists()) {
                temp.delete();
            }
        }

    }

    private File createPath(Long id) {
        if (id == null) {
            return null;
        }
        File dest = new File(this.destPath);
        if (!dest.exists()) {
            dest.mkdirs();
        }
        return new File(dest, id + ".html");
    }


    /**
     * 判断某个商品的页面是否存在
     * @param id
     * @return
     */
    public boolean exists(Long id) {
        return this.createPath(id).exists();
    }


    public void syncCreateHtml(Long id){
        ThreadUtils.execute(() -> {
            try {
                createHtml(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public void deleteHtml(Long id) {

    }
}
